<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic;


use app\common\model\InsurancePolicy;
use app\common\model\Repair;
use app\common\logic\BaseLogic;
use app\common\model\user\User;
use think\facade\Db;


/**
 * Repair逻辑
 * Class RepairLogic
 * @package app\adminapi\logic
 */
class RepairLogic extends BaseLogic
{

    /**
     * @param Repair $repair
     * @param string $receiveExpressNo
     * @notes 完成维修
     * @return Repair
     */
    public static function  finishRepair(  Repair $repair,string $receiveExpressNo)
    {
        $repair->status = Repair::STATUS_FINISH;
        $repair->finish_repari_time = date('Y-m-d H:i:s');
        $repair->receive_express_no = $receiveExpressNo;
        $repair->save();
        $insurance =  $repair->insurance()->findOrFail();

        $insurance->status = InsurancePolicy::StATUS_FINISH ;
        $insurance->save();
        return $repair;

    }

    /**
     * @param User $user
     * @param InsurancePolicy $insurancePolicy
     * @param array $imgs
     * @param string $remark
     * @notes 报修
     * @return Repair
     */
    public static function addUserRepair(User $user, InsurancePolicy $insurancePolicy, array $imgs = [], string $remark = ''):Repair
    {
        $repair = new Repair();
        $repair->user_id = $user->id;
        $repair->order_no = generate_sn(Repair::class,'order_no','R');
        $repair->insurance_policy_id = $insurancePolicy->id;
        $repair->product_type_id = $insurancePolicy->product_type_id;
        $repair->spec = $insurancePolicy->spec;
        $repair->brand = $insurancePolicy->brand;
        $repair->imei = $insurancePolicy->orderinfo->imei;
        $repair->remark = $remark;
        $repair->imgs = json_encode($imgs);
        $repair->save();
        return $repair;
    }

    /***
     * @param Repair $repair
     * @param string $expressNo
     * @param string $expressType
     * @param string $receiveExpressNo
     * @param string $receiveAddress
     * @param string $receiveConcatName
     * @param string $receiveConcatPhone
     * @notes
     * @return Repair
     */
    public static function fillInExpress(
        Repair $repair,
        string $expressNo,
        string $expressType,
        string $receiveAddress,
        string $receiveConcatName,
        string $receiveConcatPhone
    ) {
        $repair->express_no = $expressNo;
        $repair->express_type = $expressType;
        $repair->receive_address = $receiveAddress;
        $repair->receive_concate_name = $receiveConcatName;
        $repair->receive_concate_phone = $receiveConcatPhone;
        $repair->status = Repair::STATUS_REPAIRING;
        $repair->save();
        return $repair;
    }


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            Repair::create([
                'imei'                  => $params['imei'],
                'imgs'                  => $params['imgs'],
                'remark'                => $params['remark'],
                'express_no'            => $params['express_no'],
                'express_type'          => $params['express_type'],
                'receive_express_no'    => $params['receive_express_no'],
                'receive_address'       => $params['receive_address'],
                'receive_concate_name'  => $params['receive_concate_name'],
                'receive_concate_phone' => $params['receive_concate_phone']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            Repair::where('id', $params['id'])->update([
                'imei'                  => $params['imei'],
                'imgs'                  => $params['imgs'],
                'remark'                => $params['remark'],
                'express_no'            => $params['express_no'],
                'express_type'          => $params['express_type'],
                'receive_express_no'    => $params['receive_express_no'],
                'receive_address'       => $params['receive_address'],
                'receive_concate_name'  => $params['receive_concate_name'],
                'receive_concate_phone' => $params['receive_concate_phone']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public static function delete(array $params): bool
    {
        return Repair::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2025/02/14 20:10
     */
    public static function detail($params): array
    {
        return Repair::findOrEmpty($params['id'])->toArray();
    }
}